package com.hdc.hdc.warehouseManage.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hdc.hdc.warehouseManage.entity.House;
import com.hdc.hdc.warehouseManage.entity.Position;
import com.hdc.hdc.warehouseManage.entity.vo.HouseAndPositionVo;
import com.hdc.hdc.warehouseManage.service.WareHouseAndPositionServiceI;
import com.hdc.hdc.warehouseManage.utill.AjaxReturnVO;
import com.hdc.hdc.warehouseManage.utill.IsNotNullValit;

/**
 * 仓库与库位之间的关系控制类
 * @author gxy
 *	2018年4月22日
 *	hdc.warehouseManage
 *
 */
@RestController
@RequestMapping(value="/housAndPositionController")
public class HouseAndPositionController {
	
	@Autowired
	private WareHouseAndPositionServiceI wareHouseAndPositionServiceI;
	
	/*
	 * 查询仓库，库位，初始查询
	 */
	@RequestMapping(value="/getHouseAndPosition")
	public AjaxReturnVO getHouseAndPosition(@RequestParam(name="pageNum")Integer pageNum,
											@RequestParam(name="pageSize")Integer pageSize) {
		
		AjaxReturnVO vo = new AjaxReturnVO();
		
		try {
			
			vo.setOk(true);
			vo.setAttrs(this.wareHouseAndPositionServiceI.findFirstAll(pageNum - 1,pageSize));
			
		} catch (Exception e) {
			e.printStackTrace();
			vo.setMsg("查询失败:"+e.getMessage());
			vo.setOk(false);
		}
		
		return vo;
		
	}
	
	@PostMapping(value="/saveHouse")
	public AjaxReturnVO saveHouse(@RequestBody House house) {
		
		AjaxReturnVO vo = new AjaxReturnVO();
		
		try {
			
			vo.setOk(true);
			this.wareHouseAndPositionServiceI.saveHouse(house);
			
		} catch (Exception e) {
			e.printStackTrace();
			vo.setOk(false);
			vo.setMsg("保存失败:"+e.getMessage());
		}
		
		return vo;
	}
	
	@GetMapping(value="/findHouseForPage")
	public AjaxReturnVO findHouseForPage(@RequestParam(name="pageNum")Integer pageNum,
										@RequestParam(name="pageSize")Integer pageSize) {
		
		AjaxReturnVO vo = new AjaxReturnVO();
		
		try {
			
			vo.setOk(true);
			
			Page<House> houses = this.wareHouseAndPositionServiceI.findHousePage(pageNum-1, pageSize);
			
			Map<String, Object> data = new HashMap<String, Object>(30);
			
			data.put("totalElement", houses.getTotalElements());
			data.put("content", houses.getContent());
			vo.setAttrs(data);
		} catch (Exception e) {
			e.printStackTrace();
			vo.setOk(false);
			vo.setMsg("查询失败:"+e.getMessage());
		}
		
		return vo;
		
	}
	
	@GetMapping(value="/findPositionForPage")
	public AjaxReturnVO findPositionForPage(@RequestParam(name="pageNum")Integer pageNum,
										@RequestParam(name="pageSize")Integer pageSize,
										@RequestParam(name="houseId")String houseId) {
		
		AjaxReturnVO vo = new AjaxReturnVO();
		
		if (!IsNotNullValit.StringIsNotNull(houseId)) {
			
			vo.setOk(false);
			vo.setMsg("请选择仓库");
			return vo;
		}
		
		try {
			
			vo.setOk(true);
			Page<Position> positions = this.wareHouseAndPositionServiceI.findPositionPage(pageNum-1, pageSize,houseId);
			Map<String, Object> data = new HashMap<String, Object>(30);
			data.put("totalElement", positions.getTotalElements());
			data.put("content", positions.getContent());
			vo.setAttrs(data);
		} catch (Exception e) {
			e.printStackTrace();
			vo.setOk(false);
			vo.setMsg("查询失败"+e.getMessage());
		}
		
		return vo;
	}
	
	@PostMapping(value="/savePosition")
	public AjaxReturnVO savePosition(@RequestBody HouseAndPositionVo vo) {
		
		AjaxReturnVO j = new AjaxReturnVO();
		
		try {
			
			j.setOk(true);
			
			Position position = new Position();
			BeanUtils.copyProperties(vo, position);
			position.setCreateDate(new Date());
			this.wareHouseAndPositionServiceI.savePosition(position);
		} catch (Exception e) {
			e.printStackTrace();
			j.setOk(false);
			j.setMsg("查询失败:"+e.getMessage());
		}
		
		return j;
	}
	
	@GetMapping(value="/findPositionForQuery")
	public AjaxReturnVO findPositonForQuery(@RequestParam(name="houseId")String houseId) {
		
		AjaxReturnVO vo = new AjaxReturnVO();
		
		try {
			
			vo.setOk(true);
			vo.setData(this.wareHouseAndPositionServiceI.findAllPosition(houseId));
		} catch (Exception e) {
			e.printStackTrace();
			vo.setMsg("查询失败:"+e.getMessage());
			vo.setOk(false);
		}
		return vo;
	}
	
	@GetMapping(value="/getAllHouse")
	public AjaxReturnVO findAllHouse() {
		
		AjaxReturnVO vo = new AjaxReturnVO();
		
		try {
			vo.setOk(true);
			vo.setData(this.wareHouseAndPositionServiceI.findAllHouses());
		} catch (Exception e) {
			e.printStackTrace();
			vo.setFail(vo,e.getMessage());
		}
		
		return vo;
	}

}
